#define		RemoteDrv_C

#include "Global.h"

#define     C_HEAD_LOW					(REM_HEAD_LOW/TIMER_RATE)       
#define     C_HEAD_LOW_MIN				(C_HEAD_LOW * (1 - TOLERANCE))
#define     C_HEAD_LOW_MAX				(C_HEAD_LOW * (1 + TOLERANCE))

#define     C_HEAD_HIGH					(REM_HEAD_HIGH/TIMER_RATE)       
#define     C_HEAD_HIGH_MIN				(C_HEAD_HIGH * (1 - TOLERANCE))
#define     C_HEAD_HIGH_MAX				(C_HEAD_HIGH * (1 + TOLERANCE))

#define     ONE_ZERO_DEJUGE				((REM_ONE_CODE - REM_ZERO_CODE)*0.8 + REM_ZERO_CODE)
#define     C_ONE_CODE_MIN				(ONE_ZERO_DEJUGE/TIMER_RATE)
#define     C_ONE_CODE_MAX				(REM_SPACE_CODE/TIMER_RATE)


#define		C_REMODE_WAIT				0
#define		C_REMODE_HEAD_LOW			1
#define		C_REMODE_HEAD_HIGH			2
#define		C_REMODE_RECING_DATA		3
#define		C_REMODE_DATA_HIGH			4
#define		C_REMODE_END				5
#define		C_REMODE_DECODE				6

unsigned char  RemBuf[C_REMOTE_BUF_LENGTH] = {0};
unsigned char  RemNum = 0;
unsigned char  u8BitCnt = 0;
unsigned char  RecTempByte = 0;
unsigned char  RemoteDecodeTime = 0;
unsigned char  RemoteMode = 0;
unsigned char  RemPlusCnt = 0;
unsigned char  RecPin = 0xff;


/**************************************************************
//Fuction  void RecDrv_IRQ(void)
//Desrition: 红外接收中断函数，在计时中断里调用
//Input:  
//Output:
//Author:陈首敏
***************************************************************/
void RecDrv_IRQ(void)
{
	RecPin <<= 1;
	if(GetRemotePin())RecPin |= 0x01;
	RecPin &= b0000_0111;
	if(RemPlusCnt != 0xff)RemPlusCnt ++;
	if(C_REMODE_WAIT == RemoteMode)
	{// 红外空闲等待
		if(b0000_0000 == RecPin)
		{
			RemoteMode = C_REMODE_HEAD_LOW;
			RemPlusCnt = 0;
		}
	}
	else if(C_REMODE_HEAD_LOW == RemoteMode)
	{// 头码低电平接收阶段
		if(b0000_0111 == RecPin)
		{
			if((C_HEAD_LOW_MIN <= RemPlusCnt)&&(RemPlusCnt <= C_HEAD_LOW_MAX))
			{
				RemoteMode = C_REMODE_HEAD_HIGH;
				RemPlusCnt = 0;
			}
			else
			{
				RemoteMode = C_REMODE_WAIT;
			}
		}
	}
	else if(C_REMODE_HEAD_HIGH == RemoteMode)
	{// 头码高电平接收阶段
		if(b0000_0100== RecPin)
		{
			if((C_HEAD_HIGH_MIN <= RemPlusCnt)&&(RemPlusCnt <= C_HEAD_HIGH_MAX))
			{
				RemoteMode = C_REMODE_RECING_DATA;
				RemPlusCnt = 0;
				RemNum = 0;
				RecTempByte = 0;
				u8BitCnt = 0;
				RemoteDecodeTime = C_DECODE_DELAY_TIME;
			}
			else
			{
				RemoteMode = C_REMODE_WAIT;
			}
		}
	}
	else if(C_REMODE_RECING_DATA == RemoteMode)
	{// 遥控码数据接收
		if(b0000_0100== RecPin)
		{
			if(C_ONE_CODE_MAX <= RemPlusCnt)
			{// 间隔码
				
			}
			else
			{
				RemoteDecodeTime = C_DECODE_DELAY_TIME;
				RecTempByte <<= 1;
				
				if(C_ONE_CODE_MIN <= RemPlusCnt)
				{
					RecTempByte |= 0x01;
				}
				u8BitCnt ++;
				if(u8BitCnt >= 8)
				{
					RemBuf[RemNum] = RecTempByte;
					RemNum ++;				
					u8BitCnt = 0;
					RecTempByte = 0;
					if(RemNum >= C_REMOTE_BUF_LENGTH)
					{
						RemoteMode = C_REMODE_DECODE;
					}
				}
			}
			RemPlusCnt = 0;
		}
	}
	else
	{

	}
}

/**************************************************************
//Fuction  void RemoteProc(pDecodeFunc Func)
//Desrition: 红外接收主程序，再主循环调用
//Input:  解码函数指针
//Output:
//Author:陈首敏
***************************************************************/
void RemoteProc(pDecodeFunc Func)
{
	unsigned char temp;

	if(GetTimeTick(TIME_10ms))
	{
		if(RemoteDecodeTime)
		{
			RemoteDecodeTime --;
			if(RemoteDecodeTime == 0)
			{// 接收超时，进入解码阶段。
				RemoteMode = C_REMODE_DECODE;
			}
		}
	}

	if(C_REMODE_DECODE == RemoteMode)
	{
		if(RemNum)
		{
			(*Func)(RemBuf,RemNum); // 将buf和实际接收长度传给解码函数
		}
		for(temp = 0 ; temp < C_REMOTE_BUF_LENGTH; temp ++)
		{
			RemBuf[temp] = 0;
		}
		RemoteDecodeTime = 0;
		RemoteMode = C_REMODE_WAIT; // 完成解码才能进入接收阶段。
	}
}

